package com.tokenplus.thai.changepassword

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.tokenplus.thai.R
import com.tokenplus.thai.aviewmodel.LoginViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.databinding.ActivityPasswordVerifyInputBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.extend.*

/** 创建钱包-验证页 */
@SuppressLint("CustomSplashScreen")
class PasswordVerifyInputActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityPasswordVerifyInputBinding

    //登录viewModel
    private val mLoginViewModel by lazy {
        ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mLoginViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.password_verify_input.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityPasswordVerifyInputBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //输入监听
            onHandleEditInputChanged()

            //监听live data 返回
            onHandleLiveDataCallback()

            //点击确定
            mActivityBinding.submitInputWalletVerify.setOnClickListener {
                //判断助记词是否为空
                val content = mActivityBinding.editInputWalletVerify.text.toString().trim()
                if (content.isNotBlank()) {
                    val params = HashMap<String, String>()
                    params["zjc"] = content
                    startToPasswordNewInputActivity(params)

                } else {
                    Toast.makeText(
                        this,
                        getString(R.string.create_wallet_tips3),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 输入监听 */
    private fun onHandleEditInputChanged() {
        mActivityBinding.editInputWalletVerify.addTextChangedListener(object :
            TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(p0: Editable?) {
                //空格换成逗号
                val contentTemp = mActivityBinding.editInputWalletVerify.text.toString().trim()
                var content = contentTemp.replace(" ", ",")

                // 0123456789
                while (content.endsWith(",")) {
                    content = content.substring(0, content.length - 1)
                }

                //判断提交按键是否可用
                if (content.isNotBlank()) {
                    mActivityBinding.submitInputWalletVerify.setBackgroundResource(R.drawable.main_btn_hightlight)
                } else {
                    mActivityBinding.submitInputWalletVerify.setBackgroundResource(R.drawable.main_btn_ban)
                }
            }
        })
    }

    /** 监听live data 返回 */
    private fun onHandleLiveDataCallback() {

    }

}